//
//  HomeViewController.m
//  RTMLibDemo
//
//  Created by admin on 2022/3/31.
//

#import "HomeViewController.h"
#import "Masonry/Masonry.h"
#import "RTMActionView.h"
#import "ChatRoomViewController.h"
#import "PeerRoomViewController.h"
#import "UIViewController+HUD.h"
@interface HomeViewController ()

@property (nonatomic,strong)UIButton *peerBtn;
@property (nonatomic,strong)UIButton *channelBtn;
@property (nonatomic,strong)RTMActionView *actionView;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"云信令";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initView];
    
    // Do any additional setup after loading the view.
}

-(void)initView{
    
    [self.view addSubview:self.peerBtn];
    [self.view addSubview:self.channelBtn];
    [self.peerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width-50,[UIScreen mainScreen].bounds.size.height/4));
        make.top.mas_equalTo(self.view.mas_top).offset([UIScreen mainScreen].bounds.size.height/5);
        
    }];
    
    [self.channelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width-50, [UIScreen mainScreen].bounds.size.height/4));
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-[UIScreen mainScreen].bounds.size.height/5);
        
    }];
    
}
-(void)peerAction{
    
    self.actionView.titleLable.text = @"点对点消息(peerToPeer)";
    self.actionView.nameTextField.placeholder = @"请输入对方id";
    [self.actionView show];
    __weak typeof(self) weakself = self;
    [self.actionView setTextBlock:^(NSString * _Nonnull text) {
        if ([text isEqualToString:@""]) {
            
            [weakself.actionView.window.rootViewController showHint:@"对方id不能为空"];
            return;
        }
        weakself.actionView.nameTextField.text = @"";
        [weakself.actionView.nameTextField resignFirstResponder];
        [weakself.actionView cancleAct];
        PeerRoomViewController *vc = [[PeerRoomViewController alloc]init];
        vc.peerId = text;
        vc.title = [NSString stringWithFormat:@"peer:%@",text];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"";
        weakself.navigationItem.backBarButtonItem = backItem;
        [weakself.navigationController pushViewController:vc animated:YES];
        
    }];
    
}
-(void)channelAction{
    
    self.actionView.titleLable.text = @"频道消息(channel)";
    self.actionView.nameTextField.placeholder = @"请输入channelId";
    [self.actionView show];
    __weak typeof(self) weakself = self;
    [self.actionView setTextBlock:^(NSString * _Nonnull text) {
        if ([text isEqualToString:@""]) {
            
            [weakself.actionView.window.rootViewController showHint:@"channelId不能为空"];
            return;
        }
        weakself.actionView.nameTextField.text = @"";
        [weakself.actionView.nameTextField resignFirstResponder];
        [weakself.actionView cancleAct];
        ChatRoomViewController *vc = [[ChatRoomViewController alloc]init];
        vc.channelId = text;
        vc.title = [NSString stringWithFormat:@"channel:%@",text];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
        backItem.title = @"";
        weakself.navigationItem.backBarButtonItem = backItem;
        [weakself.navigationController pushViewController:vc animated:YES];
        
    }];
    
}
-(RTMActionView *)actionView{
    
    if (_actionView == nil) {
        
        _actionView = [[RTMActionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        _actionView.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.3];
    }
    
    return _actionView;
}
-(UIButton *)peerBtn{
    
    if (_peerBtn == nil) {
        
        _peerBtn = [[UIButton alloc]init];
//        _peerBtn.backgroundColor = [UIColor colorWithRed:83/255.0 green:171/255.0 blue:229/225.0 alpha:1];
        [_peerBtn setImage:[UIImage imageNamed:@"Image_Peer"] forState:UIControlStateNormal];
        [_peerBtn addTarget:self action:@selector(peerAction) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return _peerBtn;
}
-(UIButton *)channelBtn{
    
    if (_channelBtn== nil) {
        
        _channelBtn = [[UIButton alloc]init];
//        _channelBtn.backgroundColor = [UIColor colorWithRed:240/255.0 green:147/255.0 blue:142/225.0 alpha:1];
        [_channelBtn setImage:[UIImage imageNamed:@"Image_Channel"] forState:UIControlStateNormal];
        [_channelBtn addTarget:self action:@selector(channelAction) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return _channelBtn;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
